package com.bittech.client.service;

import com.bittech.client.dao.AccountDao;
import com.bittech.client.entity.Account;

import javax.swing.*;

/**
 * @Author: yuisama
 * @Date: 2019-06-12 14:21
 * @Description:
 */
public class UserReg {
    private JTextField userName;
    private JPasswordField password;
    private JTextField brief;
    private JButton regButton;
    private JTextField age;
    private JPanel regPanel;
    private AccountDao accountDao;
    private JFrame regFrame;
    private UserLogin userLogin;

    public JPanel getRegPanel() {
        return regPanel;
    }

    public void frameOpen() {
        regFrame.setVisible(true);
    }

    public void frameClosed() {
        regFrame.setVisible(false);
    }


    public UserReg() {
        regFrame = new JFrame("用户注册");
        regFrame.setContentPane(regPanel);
        regFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        regFrame.pack();
        regFrame.setVisible(true);
        accountDao = new AccountDao();
        regButton.addActionListener( (e) -> {
            Account account = new Account();
            account.setUserName(userName.getText());
            account.setPassword(String.valueOf(password.getPassword()));
            account.setAge(Short.valueOf(age.getText()));
            account.setBrief(brief.getText());
            if (accountDao.register(account)) {
                JOptionPane.showMessageDialog(null,"注册成功!","提示",
                        JOptionPane.INFORMATION_MESSAGE);
                frameClosed();
            }else {
                JOptionPane.showMessageDialog(null,"用户已注册!","提示",
                        JOptionPane.ERROR_MESSAGE);
                frameClosed();
            }
        });
    }
}
